//
//  SXYSDetailViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/30.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXYSDetailViewController.h"
#import "SXRequestManager.h"
#import "UMSocial.h"
@interface SXYSDetailViewController ()<UIWebViewDelegate>
@property(nonatomic, strong) UIWebView *webView;
@property(nonatomic, copy) NSString *shareStr;
@end

@implementation SXYSDetailViewController
- (UIWebView *)webView
{
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.delegate = self;
    }
    return _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    self.navigationController.navigationBar.hidden = NO;
    [self requestData];
    [self setNavigationBar];
    // Do any additional setup after loading the view.
}
- (void)requestData
{
    NSString *URLStr = [kYSNewsDetailURLString stringByAppendingString:[NSString stringWithFormat:@"%@.json", _idStr]];
    [SXRequestManager requestWithType:GET urlString:URLStr parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSString *htmlStr = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:dic[@"result"][@"art_url"]]] encoding:NSUTF8StringEncoding];
        _shareStr = dic[@"result"][@"art_url"];
        NSRange headRange = [htmlStr rangeOfString:@"<!DOCTYPE html>"];
        NSRange endRange = [htmlStr rangeOfString:@"</head><body>"];
        NSRange allRange = NSMakeRange(headRange.location, endRange.location + endRange.length - headRange.location);
        NSString *allString = [htmlStr substringWithRange:allRange];
        
        NSRange body = [htmlStr rangeOfString:@"<div class=\"height-shim\">"];
        NSRange end = [htmlStr rangeOfString:@"</div><p class=\"news-info\">"];
        NSRange all = NSMakeRange(body.location, end.location + end.length - body.location);
        NSString *allStr = [htmlStr substringWithRange:all];
        
        NSRange body2 = [htmlStr rangeOfString:@"<div class=\"header-img\">"];
        NSRange end2 = [htmlStr rangeOfString:@"</div><div class=\"article-content\">"];
        NSRange all2 = NSMakeRange(body2.location, end2.location + end2.length - body2.location);
        NSString *allStr2 = [htmlStr substringWithRange:all2];
        
        allString = [allString stringByAppendingFormat:@"%@%@", allStr, allStr2];
        [self.webView loadHTMLString:allString baseURL:nil];
    } error:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
// 让webView里面的超链接失效
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *requestURL = request.URL;
    if (([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString:@"https"])
        && (navigationType == UIWebViewNavigationTypeLinkClicked)) {
        return NO;
    }
    return YES;
}
- (void)setNavigationBar
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"SX-yuedufanhui"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(didClickLeft)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"SX-yuedushare"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(didClickRight)];
}
- (void)didClickLeft
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didClickRight
{
    [UMSocialConfig setFinishToastIsHidden:NO position:(UMSocialiToastPositionCenter)];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5632c41ce0f55a17390010c1"
                                      shareText:[NSString stringWithFormat:@"//分享新闻 %@ %@", _titleStr, _shareStr]
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToDouban,UMShareToRenren,UMShareToInstagram, UMShareToSms,nil]
                                       delegate:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
